import { _decorator, instantiate, Vec3, AudioClip, UITransform, AudioSource, tween, Prefab, Component } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Transition')
export class Transition extends Component {
    @property({type: Prefab})
    warningPrefab: Prefab = null

    @property({type: Prefab})
    exitPrefab: Prefab = null

    @property({type: AudioClip})
    slideAudio: AudioClip = null!

    transitionItemNum: number = 20

    start() {

    }

    /** 
     * @zh
     * 显示过渡缓动。
    */
    showTransition(transitionType: string, callback=null) {
        // 从右到左移动
        let canvasWidth = this.node.getComponent(UITransform).width
        let item = this.spawnTransitionItem(transitionType)
        item.setPosition(canvasWidth, 0, 0)
        this.node.addChild(item)
        this.node.getComponent(AudioSource).playOneShot(this.slideAudio)
        tween(item).to(1.3, {'position': new Vec3(-canvasWidth, 0, 0)})
                   .call(()=>{if (callback) callback(); item.destroy()})
                   .start()
    }

    /** 
     * @zh
     * 生成用于过渡缓动的节点。
    */
    spawnTransitionItem(transitionType: string) {
        let item = null
        if (transitionType == 'warning') {
            item = instantiate(this.warningPrefab)
        }
        else if (transitionType == 'exit') {
            item = instantiate(this.exitPrefab)
        }
        return item
    }
}


